//
//  FZEnDecode.cpp
//  FZEnDecode
//
//  Created by 李雷川 on 2019/6/11.
//  Copyright © 2019 李雷川. All rights reserved.
//

#include "FZEnDecode.hpp"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CHUNKSIZE    10                          //加密块的大小
#define FILLCHARACTERS    "??????????"            //填充内容

//文件是否已经加密
bool isEncoded(const char* filePath)
{
    size_t filenameLen = strlen(filePath);
    if (strcmp(&filePath[filenameLen-3],"ent") ==0) {
        return true;
    }
    return false;
    
}


//
bool encode(const char* filePath)
{
    if (access(filePath, 00) != 0) {//文件不存在
        return false;
    }
    
    FILE *fp = fopen(filePath, "rb+");
    if (!fp) {          //打开需要加密的文件失败
        return false;
    }
    
    long filePathLen = strlen(filePath);
    char enfilePath[512];
    memset(enfilePath, 0x00, 512);
    
    memcpy(enfilePath, filePath, filePathLen);
    memcpy(enfilePath+filePathLen, "ent", strlen("ent"));
    
    char header[CHUNKSIZE];
    memset(header, 0x00, CHUNKSIZE);
    char tail[CHUNKSIZE];
    memset(tail, 0x00, CHUNKSIZE);
    
    if (access(enfilePath, 00) == 0) {//密文块文件存在,只需要填充需要加密的文件的头尾
        fwrite(FILLCHARACTERS, sizeof(char), CHUNKSIZE, fp);
        fseek(fp, -CHUNKSIZE, SEEK_END);
        fwrite(FILLCHARACTERS, sizeof(char), CHUNKSIZE, fp);
        fclose(fp);
    }else {//密文块文件不存在,需要创建密文块文件
        FILE *enfp = fopen(enfilePath, "wb");
        if (!enfp) {        //创建加密文件失败
            return false;
        }
        fread(header, sizeof(char), CHUNKSIZE, fp);
        fseek(fp, 0, SEEK_SET);
        fwrite(FILLCHARACTERS, sizeof(char), CHUNKSIZE, fp);
        fseek(fp, -CHUNKSIZE, SEEK_END);
        fread(tail, sizeof(char), CHUNKSIZE, fp);
        fseek(fp, -CHUNKSIZE, SEEK_END);
        fwrite(FILLCHARACTERS, sizeof(char), CHUNKSIZE, fp);
        fclose(fp);
        
        fwrite(header, sizeof(char), CHUNKSIZE, enfp);
        fwrite(tail, sizeof(char), CHUNKSIZE, enfp);
        fclose(enfp);
    }
    
    return true;
    
}

//
bool decode(const char* filePath)
{
    if(access(filePath, 00) != 0){    //需要解密的文件不存在
        return false;
    }

    long filePathLen = strlen(filePath);
    char enFilePath[512];
    memset(enFilePath, 0x00, 512);
    memcpy(enFilePath, filePath, filePathLen);
    memcpy(enFilePath+filePathLen, "ent", strlen("ent"));
    if(access(enFilePath, 00) != 0){    //加密文件不存在
        return false;
    }
    
    FILE *fp = fopen(filePath, "rb+");
    if(!fp){//解密文件打开失败
        return false;
    }
    
    FILE *fpen = fopen(enFilePath, "rb");
    if(!fpen){//加密文件块打开失败
        fclose(fp);
        return false;
    }
    
    char header[CHUNKSIZE];
    memset(header, 0x00, CHUNKSIZE);
    char tail[CHUNKSIZE];
    memset(tail, 0x00, CHUNKSIZE);
    fread(header, sizeof(char), CHUNKSIZE, fpen);
    fread(tail, sizeof(char), CHUNKSIZE, fpen);
    fclose(fpen);
    
    fwrite(header, sizeof(char), CHUNKSIZE, fp);
    fseek(fp, -CHUNKSIZE, SEEK_END);
    fwrite(tail, sizeof(char), CHUNKSIZE, fp);
    fclose(fp);
    
    return true;
}

